package boardgamer.yavalath.client.game;

import android.os.Parcel;
import android.os.Parcelable;
import boardgamer.yavalath.client.YavalathPlayer;
import boardgamer.yavalath.common.YavalathPlayerState;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;

import java.util.Map;

/**
 * Parcel containing all players in the game
 *
 * @author garysoed
 */
public class GamePlayersParcel implements Parcelable {

  // Required for Parcelable implementation
  public static final Creator<GamePlayersParcel> CREATOR
      = new Creator<GamePlayersParcel>() {
        @Override
        public GamePlayersParcel createFromParcel(Parcel parcel) {
          
          Map<YavalathPlayer, YavalathPlayerState> players
              = (ImmutableMap<YavalathPlayer, YavalathPlayerState>) parcel.readSerializable();
          return new GamePlayersParcel(players);
        }

        @Override
        public GamePlayersParcel[] newArray(int i) {
          return new GamePlayersParcel[i];
        }
      };

  private final ImmutableMap<YavalathPlayer, YavalathPlayerState> players;

  public GamePlayersParcel(Map<YavalathPlayer, YavalathPlayerState> players) {
    this.players = ImmutableMap.copyOf(players);
  }
  
  public ImmutableMap<YavalathPlayer, YavalathPlayerState> getPlayers() {
    return players;
  }

  @Override
  public int describeContents() {
    return Objects.hashCode(players);
  }

  @Override
  public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeSerializable(players);
  }
}
